﻿Imports SACommon
Public Class UpdateVersion
    Private mHasApplicationUpdate As Boolean
    Private mHasInstallerUpdate As Boolean
    Private mHasHelpUpdate As Boolean

    Private mNewApplicationVersion As String

    Private mIsDownloadCompulsory As Boolean

    Private mNewInstallerVersion As String
    Private mNewInstallerMD5 As String

    Private mNewHelpVersion As String
    Private mNewHelpMD5 As String

    Private mAllowUpdate As Boolean
    Private mIsBeta As Boolean
    Private mNewType As String

    Private mHasReadDefinitionFile As Boolean
    Private mReadDefinitionFileSuccess As Boolean

#Region "Get Properties"
#Region "Folders"
    Public Shared ReadOnly Property RepositoryMainUpdateFolder As String
        Get
            Return UpdateCore.FixURLText(UpdateCore.RepositoryServer & UpdateCore.RepositoryFolder)
        End Get
    End Property
    Public ReadOnly Property RepositoryFileUpdateFolder As String
        Get
            Return UpdateCore.FixURLText(UpdateCore.FixURLText(UpdateCore.RepositoryServer & UpdateCore.RepositoryFolder) & "/UpdateFiles")
        End Get
    End Property
    Public ReadOnly Property InstallerFolder As String
        Get
            Return UpdateCore.FixFolderText(UpdateCore.FixFolderText(Application.ExecutablePath) & "Update")
        End Get
    End Property
#End Region
#Region "Files"
    Public ReadOnly Property RepositoryFileDefinitionFile As String
        Get
            Dim tUFileList As String = "FileList_" & mNewApplicationVersion
            If mIsBeta = True Then tUFileList &= "_" & mNewType
            tUFileList = tUFileList & ".html"
            Return String.Format("{0}/{1}", RepositoryMainUpdateFolder, tUFileList)
        End Get
    End Property
    Public ReadOnly Property RepositorySettingsDefinitionFile As String
        Get
            Dim tUFileList As String = "Settings_" & mNewApplicationVersion
            If mIsBeta = True Then tUFileList &= "_" & mNewType
            tUFileList = tUFileList & ".html"
            Return String.Format("{0}/{1}", RepositoryMainUpdateFolder, tUFileList)
        End Get
    End Property
    Public ReadOnly Property RepositoryNotesDefinitionFile As String
        Get
            Dim tUFileList As String = "Notes_" & mNewApplicationVersion
            If mIsBeta = True Then tUFileList &= "_" & mNewType
            tUFileList = tUFileList & ".html"
            Return String.Format("{0}/{1}", RepositoryMainUpdateFolder, tUFileList)
        End Get
    End Property
    Public ReadOnly Property RepositoryCompressedInstallerFile As String
        Get
            Const tIFile As String = "SACI.ZIP"
            Return String.Format("{0}/{1}", RepositoryMainUpdateFolder, tIFile)
        End Get
    End Property
    Public ReadOnly Property CompressedInstallerFile As String
        Get
            Const tIFile As String = "SACI.ZIP"
            Return String.Format("{0}/{1}", InstallerFolder, tIFile)
        End Get
    End Property
    Public ReadOnly Property InstallerFile As String
        Get
            Const tIFile As String = "Installer.exe"
            Return String.Format("{0}/{1}", InstallerFolder, tIFile)
        End Get
    End Property
#End Region
#Region "Versions"
    Public ReadOnly Property NewApplicationVersion As String
        Get
            Return mNewApplicationVersion
        End Get
    End Property
    Public ReadOnly Property NewInstallerVersion As String
        Get
            Return mNewInstallerVersion
        End Get
    End Property
    Public ReadOnly Property NewHelpVersion As String
        Get
            Return mNewHelpVersion
        End Get
    End Property
    Public ReadOnly Property NewInstallerMD5 As String
        Get
            Return mNewInstallerMD5
        End Get
    End Property
    Public ReadOnly Property NewHelpMD5 As String
        Get
            Return mNewHelpMD5
        End Get
    End Property
#End Region
    Public ReadOnly Property HasApplicationUpdate As Boolean
        Get
            Return mHasApplicationUpdate
        End Get
    End Property
    Public ReadOnly Property HasInstallerUpdate As Boolean
        Get
            Return mHasInstallerUpdate
        End Get
    End Property
    Public ReadOnly Property HasHelpUpdate As Boolean
        Get
            Return mHasHelpUpdate
        End Get
    End Property
    Public ReadOnly Property IsDownloadCompulsory As Boolean
        Get
            Return mIsDownloadCompulsory
        End Get
    End Property
    Public ReadOnly Property AllowUpdate As Boolean
        Get
            Return mAllowUpdate
        End Get
    End Property
    Public ReadOnly Property IsBeta As Boolean
        Get
            Return mIsBeta
        End Get
    End Property
    Public ReadOnly Property NewType As String
        Get
            Return mNewType
        End Get
    End Property
#End Region
    Sub New()
        mHasReadDefinitionFile = False
        mHasInstallerUpdate = False
        mHasHelpUpdate = False
        mHasApplicationUpdate = False
        mAllowUpdate = False
        mNewInstallerVersion = ""
        mNewApplicationVersion = ""
        UpdateCore.CurrentAppVersion = "0.0.0.0"
        UpdateCore.CurrentInstallerVersion = "0.0.0.0"
        UpdateCore.CurrentHelpVersion = "0.0.0.0"
    End Sub
    Async Function CheckUpdate() As Task(Of SAEventArgs)
        If mHasReadDefinitionFile = True Then
            If mReadDefinitionFileSuccess = True Then Return New SAEventArgs("", SAEventArgs.Results.Success)
        End If
        mHasApplicationUpdate = False
        mHasInstallerUpdate = False
        mHasHelpUpdate = False
        mIsDownloadCompulsory = False
        If UpdateCore.CheckVersionNumberFormat(UpdateCore.CurrentAppVersion) = False Then Return New SAEventArgs("AppVersion Incorrect Format", SAEventArgs.Results.Undefined)
        If UpdateCore.CheckVersionNumberFormat(UpdateCore.CurrentInstallerVersion) = False Then Return New SAEventArgs("InstallerVersion Incorrect Format", SAEventArgs.Results.Undefined)
        If UpdateCore.CheckVersionNumberFormat(UpdateCore.CurrentHelpVersion) = False Then Return New SAEventArgs("HelpVersion Incorrect Format", SAEventArgs.Results.Undefined)

        Dim tResult = Await ReadLatestVersionData()
        If tResult.Result <> SAEventArgs.Results.Success Then Return tResult
        mReadDefinitionFileSuccess = True
        mHasReadDefinitionFile = True
        Return tResult
    End Function
    Private Async Function ReadLatestVersionData() As Task(Of SAEventArgs)
        Dim TempStr As String

        Dim fd As New FileDownload
        Dim tResult = Await fd.DownloadFileToString(UpdateCore.RepositoryMainUpdateVersionFile)
        If tResult.Result <> SAEventArgs.Results.Success Then Return tResult
        TempStr = tResult.Message
        If TempStr = "" Then Return New SAEventArgs("No Content", SAEventArgs.Results.NoContent)

        Try
            Dim vf As New UpdateCore.UpdateVersion
            vf.ReadVersionData(TempStr)

            mAllowUpdate = vf.Allow

            mNewApplicationVersion = vf.CoreVersion
            mIsBeta = vf.CoreIsBeta
            mNewType = vf.CoreType

            mNewInstallerVersion = vf.InstallerVersion
            mNewInstallerMD5 = vf.InstallerMD5

            mNewHelpVersion = vf.HelpVersion
            mNewHelpMD5 = vf.HelpMD5

            mHasApplicationUpdate = UpdateCore.CheckNewerVersion(mNewApplicationVersion, UpdateCore.CurrentAppVersion)
            If mHasApplicationUpdate = False Then mIsDownloadCompulsory = False
            mHasInstallerUpdate = UpdateCore.CheckNewerVersion(mNewInstallerVersion, UpdateCore.CurrentInstallerVersion)
            mHasHelpUpdate = UpdateCore.CheckNewerVersion(mNewHelpVersion, UpdateCore.CurrentHelpVersion)
            Return New SAEventArgs("", SAEventArgs.Results.Success)
        Catch ex As Exception
            Return New SAEventArgs(ex.Message, SAEventArgs.Results.GeneralFailure)
        End Try
    End Function
    Public Async Function ReadLatestVersionNotes() As Task(Of SAEventArgs)
        If mHasReadDefinitionFile = False Then Return New SAEventArgs("Definition file has to be read", SAEventArgs.Results.GeneralFailure)
        Dim TempStr As String
        Dim fd As New FileDownload
        Dim tResult = Await fd.DownloadFileToString(RepositoryNotesDefinitionFile)
        If tResult.Result <> SAEventArgs.Results.Success Then Return tResult
        TempStr = tResult.Message
        Return New SAEventArgs(Compression.InflateString(TempStr), SAEventArgs.Results.Success)
    End Function
End Class
